package edu.chinasoft.aop01;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component(value = "people")
@Aspect
public
class People {
    private Log log = LogFactory.getLog(People.class);
    //定义切入点
    @Pointcut("execution(* edu.chinasoft.aop01.Movie.run(..))")
    private void myPointcut(){
    }
    @Before( value = "myPointcut()")
    public
    void method_before() {
        log.info( "观众全体起立，鼓掌10分钟……" );
    }
    @After( value = "myPointcut()")
    public
    void method_after() {
        log.info( "电影院员工要打扫卫生，为下一场电影做准备……" );
    }
    @AfterReturning(value = "myPointcut()")
    public
    void method_after_return() {
        log.info("观众全体起立，鼓掌5分钟……");
        log.info("开始有序离场，……");
    }
    @AfterThrowing(value = "myPointcut()",throwing = "e")
    public void method_throw(Throwable e){
        if(e instanceof PoorQualityException){
            log.info("把钱退给观众");
        }else if(e instanceof  PoorEnvironmentException){
            log.info("保安维持秩序，保证电影的观看效果……");
        }else{
            log.info("发生其他特殊情况，应急处理。");
        }
    }

    /*
        public Object
        方法名字可以随便起
        方法必须要带有参数，参数必须是ProceedingJoinPoint
       */
    public Object method_around(ProceedingJoinPoint pjp) throws Throwable {

        Object result=null;
        try {
            this.method_before();
            // 秩序目标类(电影)的方法
            result = pjp.proceed();
            this.method_after_return();
        }catch (Throwable e){
            this.method_throw(e);
        }finally {
            this.method_after();
        }

        return result;
    }
}
